package com.wrench.elevator.base.user.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.wrench.elevator.base.user.dao.UserDao;
import com.wrench.elevator.base.user.domain.User;
import com.wrench.elevator.constant.Dict;
import com.wrench.elevator.constant.Result;
import com.wrench.elevator.exception.WrenchException;
import com.wrench.elevator.util.WrenchUtil;

/**
 * @Description: UserService
 * @author yangchenfeng
 * @date Oct 16, 2017 2:08:29 PM
 * @version V1.0
 */
@Component
public class UserService
{
    
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);
    
    private static volatile int next_user_id = Dict.INIT_NEXT_USER_ID;
    
    @Resource
    private UserDao dao;
    
    @Resource
    private FaceService faceService;
    
    @Resource
    private WalletService walletService;
    
    @Resource
    private AddressService addressService;
    
    @Resource
    private PersonService personService;
    
    @Resource
    private BuildingService buildingService;
    
    /**
     * 手机登录
     * */
    public User loginByTelphone(String telphone)
        throws WrenchException
    {
        
        // 判空
        if (WrenchUtil.hasNull(telphone))
        {
            logger.error("==> param is not found , telphone = {} ", telphone);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        telphone = telphone.trim();
        
        // 判断手机格式
        if (!WrenchUtil.isVaildPhone(telphone))
        {
            logger.error("==> telphone is not vaild , telphone = {} ", telphone);
            throw new WrenchException(Result.PHONE_NUMBER_IS_NOT_VAILD_CODE, Result.PHONE_NUMBER_IS_NOT_VAILD_MESSAGE);
        }
        
        // 登录
        User user = null;
        
        try
        {
            user = dao.loginByTelphone(telphone);
        }
        catch (Exception e)
        {
            logger.error("==> user=dao.loginByTelphone(telphone) execute error ,telphone = {} , error = {}; ",
                telphone,
                e);
            throw new WrenchException(Result.DB_SELECT_ERROR_CODE, Result.DB_SELECT_ERROR_MESSAGE);
        }
        
        logger.debug("==> user=dao.loginByTelphone(telphone) execute success , telphone = {} , user = {}",
            telphone,
            user);
        
        return user;
    }
    
    /**
     * 微信登录
     * */
    public User loginByWxId(String wx_id)
        throws WrenchException
    {
        // 判空
        if (WrenchUtil.hasNull(wx_id))
        {
            logger.error("==> param is not found , wx_id = {} ", wx_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        wx_id = wx_id.trim();
        
        // 判 空格
        if (WrenchUtil.hasBlank(wx_id))
        {
            logger.error("==> string has blank , wx_id = {} ", wx_id);
            throw new WrenchException(Result.STRING_HAS_BLANK_CODE, Result.STRING_HAS_BLANK_MESSAGE);
        }
        
        // 登录
        User user = null;
        
        try
        {
            user = dao.loginByWxId(wx_id);
        }
        catch (Exception e)
        {
            logger.error("==> user=dao.loginByWxId(wx_id) execute error ,wx_id = {} , error = {}; ", wx_id, e);
            throw new WrenchException(Result.DB_SELECT_ERROR_CODE, Result.DB_SELECT_ERROR_MESSAGE);
        }
        
        logger.debug("==> user=dao.loginByWxId(wx_id) execute success , wx_id = {} , user = {}", wx_id, user);
        
        return user;
    }
    
    /**
     * 设置地址
     * 
     * @throws WrenchException
     * */
    @Transactional(rollbackFor = WrenchException.class)
    public int setAddress(String user_id, String province_id, String province_name, String city_id, String city_name,
        String district_id, String district_name, String street_name, String building_id, String building_name,
        String zhuangshu, String danyuan, String loucen, String menpaihao)
        throws WrenchException
    {
        // TODO Auto-generated method stub
        // 判空
        if (WrenchUtil.hasNull(user_id,
            province_id,
            province_name,
            city_id,
            city_name,
            district_id,
            district_name,
            street_name,
            building_id,
            building_name,
            zhuangshu,
            danyuan,
            loucen,
            menpaihao))
        {
            logger.error("==> param is not found , user_id = {} , province_id = {} , province_name = {} , city_id = {} , city_name = {} , district_id = {} , district_name = {} , street_name = {} , building_id = {} , building_name = {} , zhuangshu = {} , danyuan = {} , loucen = {} , menpaihao = {} ",
                user_id,
                province_id,
                province_name,
                city_id,
                city_name,
                district_id,
                district_name,
                street_name,
                building_id,
                building_name,
                zhuangshu,
                danyuan,
                loucen,
                menpaihao);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        user_id = user_id.trim();
        province_id = province_id.trim();
        province_name = province_name.trim();
        city_id = city_id.trim();
        city_name = city_name.trim();
        district_id = district_id.trim();
        district_name = district_name.trim();
        street_name = street_name.trim();
        building_id = building_id.trim();
        building_name = building_name.trim();
        zhuangshu = zhuangshu.trim();
        danyuan = danyuan.trim();
        loucen = loucen.trim();
        menpaihao = menpaihao.trim();
        
        // 判 空格
        if (WrenchUtil.hasBlank(user_id,
            province_id,
            province_name,
            city_id,
            city_name,
            district_id,
            district_name,
            street_name,
            building_id,
            building_name,
            zhuangshu,
            danyuan,
            loucen,
            menpaihao))
        {
            logger.error("==> string has blank , user_id = {} , province_id = {} , province_name = {} , city_id = {} , city_name = {} , district_id = {} , district_name = {} , street_name = {} , building_id = {} , building_name = {} , zhuangshu = {} , danyuan = {} , loucen = {} , menpaihao = {} ",
                user_id,
                province_id,
                province_name,
                city_id,
                city_name,
                district_id,
                district_name,
                street_name,
                building_id,
                building_name,
                zhuangshu,
                danyuan,
                loucen,
                menpaihao);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        // 判整数
        if (!WrenchUtil.areInt(user_id, province_id, city_id, district_id, building_id, loucen))
        {
            logger.error("==> string is not int , user_id = {} , province_id = {} , city_id = {} , district_id = {} , building_id = {} , loucen = {} ",
                user_id,
                province_id,
                city_id,
                district_id,
                building_id,
                loucen);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        addressService.setOldAddressStatus(user_id, Dict.ADDRESS_ADDRESS_STATUS_DELETED);
        addressService.setAddress(user_id,
            province_id,
            province_name,
            city_id,
            city_name,
            district_id,
            district_name,
            street_name,
            building_id,
            building_name,
            zhuangshu,
            danyuan,
            loucen,
            menpaihao);
        
        User user = new User();
        user.setUser_id(Integer.valueOf(user_id));
        user.setProvince_id(Byte.valueOf(province_id));
        user.setProvince_name(province_name);
        user.setCity_id(Short.valueOf(city_id));
        user.setCity_name(city_name);
        user.setDistrict_id(Short.valueOf(district_id));
        user.setDistrict_name(district_name);
        user.setStreet_name(street_name);
        user.setBuilding_id(Integer.valueOf(building_id));
        user.setBuilding_name(building_name);
        user.setZhuangshu(zhuangshu);
        user.setDanyuan(danyuan);
        user.setLoucen(Byte.valueOf(loucen));
        user.setMenpaihao(menpaihao);
        user.setAddress(WrenchUtil.concat(province_name,
            city_name,
            district_name,
            street_name,
            building_name,
            zhuangshu,
            danyuan,
            loucen,
            "层",
            menpaihao));
        user.setUpdate_time(WrenchUtil.nowTime());
        
        // 设置地址
        int commit = -1;
        
        try
        {
            commit = dao.setAddress(user);
        }
        catch (Exception e)
        {
            logger.error("==> commit=dao.setAddress(user) execute error , user = {} , error = {}; ", user, e);
            throw new WrenchException(Result.DB_UPDATE_ERROR_CODE, Result.DB_UPDATE_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.setAddress(user) execute success , but commit <=0 , commit = {} , user = {} ",
                commit,
                user);
            throw new WrenchException(Result.USER_SET_ADDRESS_FAIL_CODE, Result.USER_SET_ADDRESS_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.setAddress(user) execute success , user = {} , commit = {} ", user, commit);
        
        return commit;
    }
    
    /**
     * 手机注册
     * 
     * @throws WrenchException
     * */
    @Transactional(rollbackFor = WrenchException.class)
    public int regUserByTelphone(String telphone, String username, String face_feature, String gender, String age)
        throws WrenchException
    {
        // 判空
        if (WrenchUtil.hasNull(telphone, username, face_feature, gender, age))
        {
            logger.error("==> param is not found , telphone = {} ,username = {} ,face_feature = {} , gender = {} , age = {} ",
                telphone,
                username,
                face_feature,
                gender,
                age);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        telphone = telphone.trim();
        username = username.trim();
        face_feature = face_feature.trim();
        gender = gender.trim();
        age = age.trim();
        
        // 判空格
        if (WrenchUtil.hasNull(telphone, username, gender, age))
        {
            logger.error("==> param is not found , telphone = {} ,username = {} , gender = {} , age = {} ",
                telphone,
                username,
                gender,
                age);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        // 判断手机格式
        if (!WrenchUtil.isVaildPhone(telphone))
        {
            logger.error("==> telphone is not vaild , telphone = {} ", telphone);
            throw new WrenchException(Result.PHONE_NUMBER_IS_NOT_VAILD_CODE, Result.PHONE_NUMBER_IS_NOT_VAILD_MESSAGE);
        }
        
        // 判断性别
        if (!WrenchUtil.isVaildGender(gender))
        {
            logger.error("==> gender is not vaild , gender = {} ", gender);
            throw new WrenchException(Result.GENDER_IS_NOT_VAILD_CODE, Result.GENDER_IS_NOT_VAILD_MESSAGE);
        }
        
        // 判断年龄是否整数
        if (!WrenchUtil.areInt(age))
        {
            logger.error("==> string is not int , age = {} ", age);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        String identity = Dict.FACE_IDENTITY_MASTER;
        int user_id = getNextUserId();
        int face_id = faceService.getNextFaceId();
        // 录脸
        int faceService_commit =
            faceService.addFace(String.valueOf(user_id), String.valueOf(face_id), identity, face_feature, age, gender);
        // 添加钱包
        int walletService_commit = walletService.addWallet(user_id);
        
        // 注册用户
        int commit = -1;
        User user = null;
        if (faceService_commit > 0 && walletService_commit > 0)
        {
            user = new User();
            user.setUser_id(user_id);
            user.setFace_id(face_id);
            user.setUsername(username);
            user.setTelphone(telphone);
            String now = WrenchUtil.nowTime();
            user.setInput_time(now);
            user.setUpdate_time(now);
            user.setIs_locked(Dict.ACCOUNT_UNLOCKED);
            
            try
            {
                commit = dao.regUserByTelphone(user);
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block
                logger.error("==> commit=dao.regUserByTelphone(user) execute error ,user = {} , error = {}; ", user, e);
                throw new WrenchException(Result.DB_INSERT_ERROR_CODE, Result.DB_INSERT_ERROR_MESSAGE);
            }
            
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.regUserByTelphone(user) execute success , but commit <=0 , commit = {} , user = {} ",
                commit,
                user);
            throw new WrenchException(Result.USER_REG_BY_TELPHONE_FAIL_CODE, Result.USER_REG_BY_TELPHONE_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.regUserByTelphone(user) execute success , commit = {} , user = {} ", commit, user);
        
        return commit;
    }
    
    /**
     * 微信注册
     * 
     * @throws WrenchException
     * */
    @Transactional(rollbackFor = WrenchException.class)
    public int regUserByWxId(String wx_id, String username, String face_feature, String gender, String age)
        throws WrenchException
    {
        // 判空
        if (WrenchUtil.hasNull(wx_id, username, face_feature, gender, age))
        {
            logger.error("==> param is not found , wx_id = {} ,username = {} ,face_feature = {} , gender = {} , age = {} ",
                wx_id,
                username,
                face_feature,
                gender,
                age);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        wx_id = wx_id.trim();
        username = username.trim();
        face_feature = face_feature.trim();
        gender = gender.trim();
        age = age.trim();
        
        // 判空格
        if (WrenchUtil.hasNull(wx_id, username, gender, age))
        {
            logger.error("==> param is not found , wx_id = {} ,username = {} , gender = {} , age = {} ",
                wx_id,
                username,
                gender,
                age);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        // 判断性别
        if (!WrenchUtil.isVaildGender(gender))
        {
            logger.error("==> gender is not vaild , gender = {} ", gender);
            throw new WrenchException(Result.GENDER_IS_NOT_VAILD_CODE, Result.GENDER_IS_NOT_VAILD_MESSAGE);
        }
        
        // 判断年龄是否整数
        if (!WrenchUtil.areInt(age))
        {
            logger.error("==> string is not int , age = {} ", age);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        String identity = Dict.FACE_IDENTITY_MASTER;
        int user_id = getNextUserId();
        int face_id = faceService.getNextFaceId();
        
        // 录脸
        int faceService_commit =
            faceService.addFace(String.valueOf(user_id), String.valueOf(face_id), identity, face_feature, age, gender);
        
        int walletService_commit = walletService.addWallet(user_id);
        
        // 注册用户
        int commit = -1;
        User user = null;
        if (faceService_commit > 0 && walletService_commit > 0)
        {
            user = new User();
            user.setUser_id(user_id);
            user.setFace_id(face_id);
            user.setUsername(username);
            user.setWx_id(wx_id);
            String now = WrenchUtil.nowTime();
            user.setInput_time(now);
            user.setUpdate_time(now);
            user.setIs_locked(Dict.ACCOUNT_UNLOCKED);
            
            try
            {
                commit = dao.regUserByWxId(user);
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block
                logger.error("==> commit=dao.regUserByWxId(user) execute error ,user = {} , error = {}; ", user, e);
                throw new WrenchException(Result.DB_INSERT_ERROR_CODE, Result.DB_INSERT_ERROR_MESSAGE);
            }
            
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.regUserByWxId(user) execute success , but commit <=0 , commit = {} , user = {} ",
                commit,
                user);
            throw new WrenchException(Result.USER_REG_BY_TELPHONE_FAIL_CODE, Result.USER_REG_BY_TELPHONE_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.regUserByWxId(user) execute success , commit = {} , user = {} ", commit, user);
        
        return commit;
    }
    
    @Transactional(rollbackFor = WrenchException.class)
    public int regUserByTelphoneCloud(String telphone, String username, String person_id)
        throws WrenchException
    {
        // 判空
        if (WrenchUtil.hasNull(telphone, username, person_id))
        {
            logger.error("==> param is not found , telphone = {} ,username = {} , person_id = {} ",
                telphone,
                username,
                person_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        telphone = telphone.trim();
        username = username.trim();
        person_id = person_id.trim();
        
        // 判断手机格式
        if (!WrenchUtil.isVaildPhone(telphone))
        {
            logger.error("==> telphone is not vaild , telphone = {} ", telphone);
            throw new WrenchException(Result.PHONE_NUMBER_IS_NOT_VAILD_CODE, Result.PHONE_NUMBER_IS_NOT_VAILD_MESSAGE);
        }
        
        int user_id = getNextUserId();
        walletService.addWallet(user_id);
        String identity = Dict.PERSON_IDENTITY_MASTER;
        String input_time = WrenchUtil.nowTime();
        personService.addPerson(String.valueOf(user_id), person_id, identity, input_time);
        
        User user = new User();
        user.setUser_id(user_id);
        user.setInput_time(input_time);
        user.setUpdate_time(input_time);
        user.setIs_locked(Dict.ACCOUNT_UNLOCKED);
        user.setUsername(username);
        user.setTelphone(telphone);
        
        int commit = -1;
        try
        {
            commit = dao.regUserByTelphone(user);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            logger.error("==> commit=dao.regUserByTelphone(user) execute error ,user = {} , error = {}; ", user, e);
            throw new WrenchException(Result.DB_INSERT_ERROR_CODE, Result.DB_INSERT_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.regUserByTelphone(user) execute success , but commit <=0 , commit = {} , user = {} ",
                commit,
                user);
            throw new WrenchException(Result.USER_REG_BY_TELPHONE_FAIL_CODE, Result.USER_REG_BY_TELPHONE_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.regUserByTelphone(user) execute success , commit = {} , user = {} ", commit, user);
        
        return commit;
        
    }
    
    @Transactional(rollbackFor = WrenchException.class)
    public int regUserByWxIdCloud(String wx_id, String username, String person_id)
        throws WrenchException
    {
        // 判空
        if (WrenchUtil.hasNull(wx_id, username, person_id))
        {
            logger.error("==> param is not found , wx_id = {} ,username = {} , person_id = {} ",
                wx_id,
                username,
                person_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        wx_id = wx_id.trim();
        username = username.trim();
        person_id = person_id.trim();
        
        int user_id = getNextUserId();
        
        walletService.addWallet(user_id);
        String identity = Dict.PERSON_IDENTITY_MASTER;
        String input_time = WrenchUtil.nowTime();
        personService.addPerson(String.valueOf(user_id), person_id, identity, input_time);
        
        User user = new User();
        user.setUser_id(user_id);
        user.setInput_time(input_time);
        user.setUpdate_time(input_time);
        user.setIs_locked(Dict.ACCOUNT_UNLOCKED);
        user.setUsername(username);
        user.setWx_id(wx_id);
        
        int commit = -1;
        try
        {
            commit = dao.regUserByWxId(user);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            logger.error("==> commit=dao.regUserByWxId(user) execute error ,user = {} , error = {}; ", user, e);
            throw new WrenchException(Result.DB_INSERT_ERROR_CODE, Result.DB_INSERT_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.regUserByWxId(user) execute success , but commit <=0 , commit = {} , user = {} ",
                commit,
                user);
            throw new WrenchException(Result.USER_REG_BY_WXID_FAIL_CODE, Result.USER_REG_BY_WXID_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.regUserByWxId(user) execute success , commit = {} , user = {} ", commit, user);
        
        return commit;
        
    }
    
    /**
     * 设置地址
     * 
     * @throws WrenchException
     * */
    @Transactional(rollbackFor = WrenchException.class)
    public int setAddressCloud(String user_id, String province_id, String province_name, String city_id,
        String city_name, String district_id, String district_name, String street_name, String building_id,
        String building_name, String zhuangshu, String danyuan, String loucen, String menpaihao)
        throws WrenchException
    {
        // TODO Auto-generated method stub
        // 判空
        if (WrenchUtil.hasNull(user_id,
            province_id,
            province_name,
            city_id,
            city_name,
            district_id,
            district_name,
            street_name,
            building_id,
            building_name,
            zhuangshu,
            danyuan,
            loucen,
            menpaihao))
        {
            logger.error("==> param is not found , user_id = {} , province_id = {} , province_name = {} , city_id = {} , city_name = {} , district_id = {} , district_name = {} , street_name = {} , building_id = {} , building_name = {} , zhuangshu = {} , danyuan = {} , loucen = {} , menpaihao = {} ",
                user_id,
                province_id,
                province_name,
                city_id,
                city_name,
                district_id,
                district_name,
                street_name,
                building_id,
                building_name,
                zhuangshu,
                danyuan,
                loucen,
                menpaihao);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        user_id = user_id.trim();
        province_id = province_id.trim();
        province_name = province_name.trim();
        city_id = city_id.trim();
        city_name = city_name.trim();
        district_id = district_id.trim();
        district_name = district_name.trim();
        street_name = street_name.trim();
        building_id = building_id.trim();
        building_name = building_name.trim();
        zhuangshu = zhuangshu.trim();
        danyuan = danyuan.trim();
        loucen = loucen.trim();
        menpaihao = menpaihao.trim();
        
        // 判 空格
        if (WrenchUtil.hasBlank(user_id,
            province_id,
            province_name,
            city_id,
            city_name,
            district_id,
            district_name,
            street_name,
            building_id,
            building_name,
            zhuangshu,
            danyuan,
            loucen,
            menpaihao))
        {
            logger.error("==> string has blank , user_id = {} , province_id = {} , province_name = {} , city_id = {} , city_name = {} , district_id = {} , district_name = {} , street_name = {} , building_id = {} , building_name = {} , zhuangshu = {} , danyuan = {} , loucen = {} , menpaihao = {} ",
                user_id,
                province_id,
                province_name,
                city_id,
                city_name,
                district_id,
                district_name,
                street_name,
                building_id,
                building_name,
                zhuangshu,
                danyuan,
                loucen,
                menpaihao);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        // 判整数
        if (!WrenchUtil.areInt(user_id, province_id, city_id, district_id, building_id, loucen))
        {
            logger.error("==> string is not int , user_id = {} , province_id = {} , city_id = {} , district_id = {} , building_id = {} , loucen = {} ",
                user_id,
                province_id,
                city_id,
                district_id,
                building_id,
                loucen);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        addressService.setOldAddressStatus(user_id, Dict.ADDRESS_ADDRESS_STATUS_DELETED);
        addressService.setAddress(user_id,
            province_id,
            province_name,
            city_id,
            city_name,
            district_id,
            district_name,
            street_name,
            building_id,
            building_name,
            zhuangshu,
            danyuan,
            loucen,
            menpaihao);
        
        String groupId = buildingService.getGroupId(building_id);
        logger.debug("---------------------------------------------------------------> groupId="+groupId);
        // 设置新地址 把该person放在该地址所在的group内
        if (WrenchUtil.hasNull(groupId)||groupId.trim().length()==0)
        {
            List<String> person_id = personService.getPersonId(user_id);
            String person_id0 = person_id.get(0);
            
            String group_id = WrenchUtil.createGroupByPersonId(person_id0, building_name);
            for (int i = 1; i < person_id.size(); i++)
            {
                WrenchUtil.addPersonIdToGroup(person_id.get(i), group_id);
            }
            logger.debug("if***************************************************");
            
            buildingService.setGroupId(building_id, group_id);
        }
        else
        {
            logger.debug("else***************************************************");
            List<String> person_id = personService.getPersonId(user_id);
            for (int i = 1; i < person_id.size(); i++)
            {
                WrenchUtil.addPersonIdToGroup(person_id.get(i), groupId);
            }
            
        }
        
        User user = new User();
        user.setUser_id(Integer.valueOf(user_id));
        user.setProvince_id(Byte.valueOf(province_id));
        user.setProvince_name(province_name);
        user.setCity_id(Short.valueOf(city_id));
        user.setCity_name(city_name);
        user.setDistrict_id(Short.valueOf(district_id));
        user.setDistrict_name(district_name);
        user.setStreet_name(street_name);
        user.setBuilding_id(Integer.valueOf(building_id));
        user.setBuilding_name(building_name);
        user.setZhuangshu(zhuangshu);
        user.setDanyuan(danyuan);
        user.setLoucen(Byte.valueOf(loucen));
        user.setMenpaihao(menpaihao);
        user.setAddress(WrenchUtil.concat(province_name,
            city_name,
            district_name,
            street_name,
            building_name,
            zhuangshu,
            danyuan,
            loucen,
            "层",
            menpaihao));
        user.setUpdate_time(WrenchUtil.nowTime());
        
        // 设置地址
        int commit = -1;
        
        try
        {
            commit = dao.setAddress(user);
        }
        catch (Exception e)
        {
            logger.error("==> commit=dao.setAddress(user) execute error , user = {} , error = {}; ", user, e);
            throw new WrenchException(Result.DB_UPDATE_ERROR_CODE, Result.DB_UPDATE_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.setAddress(user) execute success , but commit <=0 , commit = {} , user = {} ",
                commit,
                user);
            throw new WrenchException(Result.USER_SET_ADDRESS_FAIL_CODE, Result.USER_SET_ADDRESS_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.setAddress(user) execute success , user = {} , commit = {} ", user, commit);
        
        return commit;
    }
    
    /**
     * 获取下一个USER_ID
     * 
     * @throws WrenchException
     * */
    public int getNextUserId()
        throws WrenchException
    {
        if (next_user_id == Dict.INIT_NEXT_USER_ID)
        {
            try
            {
                next_user_id = dao.getNextUserId();
            }
            catch (Exception e)
            {
                logger.error("==> next_user_id=dao.getNextUserId() execute error ,next_user_id = {} , error = {}; ",
                    next_user_id,
                    e);
                throw new WrenchException(Result.DB_SELECT_ERROR_CODE, Result.DB_SELECT_ERROR_MESSAGE);
            }
            
        }
        else
            next_user_id++;
        logger.debug("==>  next_user_id = {} ", next_user_id);
        return next_user_id;
    }
    
}
